#!usr/bin/env python
# -*- coding:utf-8 -*-
# user nanme: Mr.zhang
# data1 time : 2022/8/10   3:33 下午
# project   : monitor_keyboard.py
# IDE       : PyCharm
# email     :17600960626@163.com


import logging
import subprocess
import os
import sys
from keyboard_monitor.connet_logic import usb_dev, wifi_ip, unauths, stops
from lib.read_sql import db

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)

from lib.read_ip import ip_list
from driver.conf_devices import devices


def dev_status(devices):
    #处理连接逻辑
    adb ='adb disconnect'
    subprocess.getoutput(adb)
    cmd ='adb devices'
    s=subprocess.getoutput(cmd)
    a = s.strip().split()[4:]
    try:
        for i,j in zip(a[1::2],devices):
            if i == 'device':
                usb_dev(j)
            elif i == 'offline':
                sql_ips = "UPDATE ip_tab set `code`=1 WHERE devices='%s'" % j
                db.updateDb(sql_ips)
                wifi_ip(j)
            elif i =='unauthorized':
                unauths(j)
    except Exception as TypeError:
                logging.error('连接异常，请检查',TypeError)



if __name__ == '__main__':
    devlist=devices()
    ip_list(devlist.devlists())
    stops(devlist.devlists())
    dev_status(devlist.devlists())